package com.liveneo.adapter.mobile.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.liveneo.adapter.base.entity.PrpmCaseInfo;
import com.liveneo.adapter.base.entity.PrpmImageCheckHis;
import com.liveneo.adapter.base.entity.PrpmUser;
import com.liveneo.adapter.base.mapper.PrpmImageCheckHisMapper;
import com.liveneo.adapter.base.mapper.PrpmImageCheckMapper;
import com.liveneo.adapter.mobile.model.PrpmImageCheck;
import com.liveneo.adapter.mobile.service.CaseInfoService;
import com.liveneo.adapter.mobile.service.ImageCheckService;
import com.liveneo.adapter.mobile.util.Constant;

@Service
@SuppressWarnings("rawtypes")
public class ImageCheckServiceImpl implements ImageCheckService {
    @Autowired
    private PrpmImageCheckMapper    prpmImageCheckMapper;
    @Autowired
    private PrpmImageCheckHisMapper prpmImageCheckHisMapper;
    @Autowired
    private CaseInfoService         caseInfoService;

    /**
     * 已处理
     */
    @Override
    public List<PrpmImageCheck> selectImageCheckYes(int pageNum, int pageSize, Map imgCheckMapYes) {
        PageHelper.startPage(pageNum, pageSize);
        return prpmImageCheckMapper.selectImageCheckYes(imgCheckMapYes);
    }

    /**
     * 未处理
     */
    @Override
    public List<PrpmImageCheck> selectImageCheckNo(int pageNum, int pageSize, Map imgCheckMapNo) {
        PageHelper.startPage(pageNum, pageSize);
        return prpmImageCheckMapper.selectImageCheckNo(imgCheckMapNo);
    }

    @Override
    public int save(PrpmImageCheck prpmImageCheck) throws Exception {
        return prpmImageCheckMapper.insert(prpmImageCheck);
    }

    @Override
    public int updateById(PrpmImageCheck prpmImageCheck) throws Exception {
        return prpmImageCheckMapper.updateByPrimaryKey(prpmImageCheck);
    }

    @Override
    public PrpmImageCheck selectByCaseUuid(String caseUuid) throws Exception {
        return prpmImageCheckMapper.selectByCaseUuid(caseUuid);
    }

    @Override
    public void updateImgCheckByCaseUuid(String caseUuid, PrpmUser user, PrpmImageCheck imgCheck) {
        // 更新图片查勘案件处理状态
        PrpmImageCheck parmImgCheck = new PrpmImageCheck();
        parmImgCheck.setCaseUuid(caseUuid);
        parmImgCheck.setDealFlag("1");
        prpmImageCheckMapper.updateByCaseUuid(parmImgCheck);
        // 增加图片查勘案件处理痕迹
        PrpmImageCheckHis parmImgCheckHis = new PrpmImageCheckHis();
        parmImgCheckHis.setImageCheckUuid(imgCheck.getUuid());
        parmImgCheckHis.setCaseUuid(caseUuid);
        parmImgCheckHis.setCaseNo(imgCheck.getCaseNo());
        parmImgCheckHis.setUserUuid(user.getUserId());
        parmImgCheckHis.setDealFlag("1");
        parmImgCheckHis.setDealTime(new Date());
        prpmImageCheckHisMapper.insert(parmImgCheckHis);
    }

    @Override
    public void saveImgcheckAndUpdCase(PrpmImageCheck pCheck, PrpmCaseInfo caseInfo, String caseUuid, String uuid) throws Exception {
        if (pCheck != null) {
            pCheck.setCaseNo(caseInfo.getCaseNo());
            pCheck.setCaseUuid(caseUuid);
            pCheck.setCheckUserUuid(uuid);
            pCheck.setCreateTime(new Date());
            pCheck.setDealFlag("0");
            prpmImageCheckMapper.updateByPrimaryKey(pCheck);
        } else {
            PrpmImageCheck prpmImageCheck = new PrpmImageCheck();
            prpmImageCheck.setCaseNo(caseInfo.getCaseNo());
            prpmImageCheck.setCaseUuid(caseUuid);
            prpmImageCheck.setCheckUserUuid(uuid);
            prpmImageCheck.setCreateTime(new Date());
            prpmImageCheck.setDealFlag("0");
            prpmImageCheck.setUuid(Constant.MYSQL_UUID);
            try {
                prpmImageCheckMapper.insert(prpmImageCheck);
            } catch (Exception e) {
            }
        }
        PrpmCaseInfo prpmCaseInfo = caseInfoService.selectByPrimaryKey(caseUuid);
        prpmCaseInfo.setCaseStatus(Constant.CASE_UPLOADFINISH);
        caseInfoService.updateByPrimaryKey(prpmCaseInfo);
    }

    @Override
    public int getSelfImgCaseNum(List<String> compUuidlist) {
        return prpmImageCheckMapper.getSelfImgCaseNum(compUuidlist);
    }

    @Override
    public int getSurveyImgCaseNum(List<String> compUuidlist) {
        return prpmImageCheckMapper.getSurveyImgCaseNum(compUuidlist);
    }

    @Override
    public int updateDealingByCaseUuid(PrpmImageCheck imgCheck) throws Exception {
        return prpmImageCheckMapper.updateDealingByCaseUuid(imgCheck);
    }
}
